package ui;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.widget.ExpandableListView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sxpjyy.monkey.mobilenurse.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import adapters.JYJGListAdapter_2;
import mytools.MyBaseActivity;
import objects.InPatient;
import objects.LisJYJG;
import objects.LisJYXM;
import service.WebService;

public class ReportJYActivity_2 extends MyBaseActivity implements SwipeRefreshLayout.OnRefreshListener {

    private ExpandableListView mJYExpandableListView;
    private SwipeRefreshLayout mJYSwipeRefreshLayout;


    private static ProgressDialog progressDialog = null;

    private String mJyxmResult = null;
    private String mJyjgResult = null;
    private List<LisJYXM> mJyxmList;
    private List<LisJYJG> mJyjgList;

    boolean[] isTaskExecuting = {false, false};

    private JYJGListAdapter_2 adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_report_jy_2);
        onBindUI();

        mJYSwipeRefreshLayout.setColorSchemeResources(R.color.holo_green_dark, R.color.holo_green_light, R.color.holo_orange_light, R.color.holo_red_light);
        mJYSwipeRefreshLayout.setOnRefreshListener(this);
        adapter = new JYJGListAdapter_2(this);
        mJYExpandableListView.setAdapter(adapter);
    }

    @Override
    protected void onResume() {
        super.onResume();
        doFlushData(true);
    }

    private void onBindUI() {
        mJYSwipeRefreshLayout = findViewById(R.id.jy_swipe_layout);
        mJYExpandableListView = findViewById(R.id.jy_expandable_lv);
    }

    @Override
    public void onRefresh() {
        doFlushData(false);
    }

    private void doFlushData(boolean isShowProgressBar) {
        InPatient inPatient = getMyApplication().getInPatient();
        if (inPatient != null && inPatient.getZYHM() != null) {
            String ZYHM = inPatient.getZYHM();
            if (ZYHM != null && !ZYHM.equals("")) {
                if (isShowProgressBar) {
                    showProgressBar(this);
                }
                String[] parms = {ZYHM};
                new MyJYXMTask().execute(parms);
                new MyJYJGTask().execute(parms);
            }
        } else {
            Toast.makeText(ReportJYActivity_2.this, "获取病人数据失败！", Toast.LENGTH_SHORT).show();
        }
    }


    //显示进度对话框
    private static void showProgressBar(Context context) {
        progressDialog = new ProgressDialog(context);
        progressDialog.setMessage("正在获取信息。。。");
        progressDialog.setCancelable(false);
        progressDialog.show();
    }

    //取消进度对话框
    private static void cancelProgressBar() {
        if (progressDialog != null && progressDialog.isShowing()) {
            progressDialog.cancel();
            progressDialog = null;
        }
    }

    /**
     * 获取检验项目
     */
    private class MyJYXMTask extends AsyncTask<String, Integer, String> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            isTaskExecuting[0] = true;
        }

        @Override
        protected String doInBackground(String... params) {
            WebService jyxmWebService = new WebService(ReportJYActivity_2.this);
            jyxmWebService.setMETHOD_NAME("getJyxmList");
            String result;
            jyxmWebService.setPARMS(params);
            result = jyxmWebService.getServerData();
            if (result == null || result.equals("null") || result.equals("") || result.equals("[]")) {
                return "获取检验项目列表失败！";
            } else {
                if (!result.equals(mJyxmResult)) {
                    mJyxmResult = result;
                    Gson gson = new Gson();
                    mJyxmList = gson.fromJson(mJyxmResult, new TypeToken<List<LisJYXM>>() {
                    }.getType());
                    if (mJyxmList != null && mJyxmList.size() > 0) {
                        return null;
                    } else {
                        return "获取检验项目列表为空！";
                    }
                } else {
                    return "数据未发生变化!";
                }
            }
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            isTaskExecuting[0] = false;
            if (s != null) {
                Toast.makeText(ReportJYActivity_2.this, s, Toast.LENGTH_SHORT).show();
            } else {
                doLoadData();
            }
        }
    }

    /**
     * 获取检验结果
     */
    private class MyJYJGTask extends AsyncTask<String, Integer, String> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            isTaskExecuting[1] = true;
        }

        @Override
        protected String doInBackground(String... params) {
            WebService jyjgWebService = new WebService(ReportJYActivity_2.this);
            jyjgWebService.setMETHOD_NAME("getJyjgList");
            String result;
            jyjgWebService.setPARMS(params);
            result = jyjgWebService.getServerData();
            if (result == null || result.equals("null") || result.equals("") || result.equals("[]")) {
                return "获取检验结果失败!";
            } else {
                if (!result.equals(mJyjgResult)) {
                    mJyjgResult = result;
                    Gson gson = new Gson();
                    mJyjgList = gson.fromJson(mJyjgResult, new TypeToken<List<LisJYJG>>() {
                    }.getType());
                    if (mJyjgList != null && mJyjgList.size() > 0) {
                        return null;
                    } else {
                        return "获取检验结果为空!";
                    }
                } else {
                    return "检验结果数据未发生变化!";
                }
            }
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            isTaskExecuting[1] = false;
            if (s != null) {
                Toast.makeText(ReportJYActivity_2.this, s, Toast.LENGTH_SHORT).show();
            } else {
                doLoadData();
            }
        }
    }

    private void doLoadData() {
        if (!isTaskExecuting[0] && !isTaskExecuting[1]) {
            cancelProgressBar();
            Map<LisJYXM, List<LisJYJG>> dataSets = new HashMap<>();
            List<LisJYJG> tempList = new ArrayList<>(mJyjgList);
            for (LisJYXM jyxm : mJyxmList
                    ) {
                List<LisJYJG> mList = new ArrayList<>();
                for (int i = tempList.size(); i > 0; i--) {
                    if (tempList.get(i - 1).getBGDH().equals(jyxm.getBGDH())) {
                        mList.add(tempList.get(i - 1));
                        tempList.remove(i - 1);
                    }
                }
                dataSets.put(jyxm, mList);
            }
            if (dataSets.size() > 0) {
                adapter.setDataSets(dataSets);
                adapter.notifyDataSetChanged();
            }
        }

    }
}
